package com.rescue.controller.user;

import com.rescue.forum.dto.IntegralQueryDTO;
import com.rescue.forum.dto.MsgPageQueryDTO;
import com.rescue.forum.dto.UserUpdateInfoDAO;
import com.rescue.forum.entity.ForumComment;
import com.rescue.forum.entity.UserInfo;
import com.rescue.forum.vo.MessageCountVO;
import com.rescue.forum.vo.UserDataVO;
import com.rescue.result.PageResult;
import com.rescue.result.Result;
import com.rescue.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/user/forum/ucenter")
@RestController
@Api(tags = "用户中心相关接口")
@Slf4j
public class UserCenterController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private UserIntegralRecordService userIntegralRecordService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private RescueRecordService rescueRecordService;
    /**
     * 根据userId获取用户信息
     *
     * @param userId
     * @return
     */
    @ApiOperation("根据userId获取用户信息")
    @GetMapping("/getUserInfo")
    private Result<UserInfo> getByUserId(Integer userId) {
        log.info("根据userId获取用户信息");
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return Result.success(userInfo);
    }

    /**
     * 修改用户信息
     * @param userUpdateInfoDAO
     * @return
     */
    @ApiOperation("修改用户信息")
    @GetMapping("/updateUserInfo")
    private Result updateUserInfo(UserUpdateInfoDAO userUpdateInfoDAO) {
        log.info("修改用户信息{}",userUpdateInfoDAO);
        userInfoService.updateUserInfo(userUpdateInfoDAO);
        return Result.success();
    }

    /**
     * 获取用户发帖
     * @param userId
     * @param type
     * @return
     */
    @ApiOperation("获取用户发帖")
    @GetMapping("/loadUserArticle")
    private Result<PageResult> loadUserArticle(Integer userId,Integer type){
        log.info("获取用户发帖{},{}",userId,type);
        PageResult pageResult = new PageResult();
        if(type == 0){
            //查询用户发布的
            pageResult = articleService.getUserIssue(userId);
        }else if(type == 1){
            //查询用户评论过的
            pageResult = articleService.getUserComment(userId);
        }else if(type == 2){
            //查询用户点赞过的
            pageResult = articleService.getUserLike(userId);
        }
        return Result.success(pageResult);
    }

    /**
     * 获取消息数
     * @return
     */
    @ApiOperation("获取消息数")
    @GetMapping("/getMessageCount")
    private Result<MessageCountVO> getMessageCount(){
        log.info("获取消息数量");
        MessageCountVO messageCountVO = userMessageService.getMessageCount();
        return Result.success(messageCountVO);
    }

    /**
     * 分页查找消息
     * @param msgPageQueryDTO
     * @return
     */
    @ApiOperation("分页查找消息")
    @GetMapping("/loadMessageList")
    private Result<PageResult> pageMsg(MsgPageQueryDTO msgPageQueryDTO){
        log.info("分页查找{}",msgPageQueryDTO);
        PageResult pageResult = userMessageService.page(msgPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询积分使用记录
     * @param integralQueryDTO
     * @return
     */
    @ApiOperation("查询积分使用记录")
    @GetMapping("/loadUserIntegralRecord")
    private Result<PageResult> pageIntegralRecord(IntegralQueryDTO integralQueryDTO){
        log.info("查询积分使用记录{}",integralQueryDTO);
        PageResult pageResult = userIntegralRecordService.page(integralQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 用户删除文章
     * @param articleId
     * @return
     */
    @ApiOperation("用户删除文章")
    @DeleteMapping("/deleteArticle")
    private Result deleteArticle(String articleId){
        log.info("用户删除文章{}",articleId);
        articleService.userDeleteById(articleId);
        return Result.success();
    }

    /**
     * 获取当前用户的评论
     * @return
     */
    @ApiOperation("获取当前用户的评论")
    @GetMapping("/getUserComment")
    private Result<List<ForumComment>> getCommentByUserId(){
        log.info("获取用户的评论");
        List<ForumComment> p = commentService.getCommentByUserId();
        return Result.success(p);
    }

    /**
     * 根据用户id统计数据
     * @param userId
     * @return
     */
    @ApiOperation("根据用户id统计数据")
    @GetMapping("/getUserData")
    private Result<UserDataVO> getUserData(Integer userId){
        log.info("获取用户数据id：{}",userId);
        UserDataVO userDataVO = rescueRecordService.getUserData(userId);
        return Result.success(userDataVO);
    }
}
